MapService.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control.GetFeature, {
	type        : OpenLayers.Control.TYPE_TOOL,
	protocol    : new OpenLayers.Protocol.HTTP({
		readWithPOST : true,
		url          : "proxy/ws-duploclique/feicao/selecionar",
		format       : new MapService.Format.WktArray(),
	    read         : function(options) {
	        OpenLayers.Protocol.prototype.read.apply(this, arguments);
	        options = options || {};
	        options.params = OpenLayers.Util.applyDefaults(
	            options.params, this.options.params);
	        options = OpenLayers.Util.applyDefaults(options, this.options);
	        if (options.filter && this.filterToParams) {
	            options.params = this.filterToParams(
	                options.filter, options.params
	            );
	        }
	        var readWithPOST = (options.readWithPOST !== undefined) ? options.readWithPOST : this.readWithPOST;
	        var resp = new OpenLayers.Protocol.Response({requestType: "read"});
	        if(readWithPOST) {
	            var headers = options.headers || {};
	            headers["Content-Type"] = "application/json";
	            resp.priv = OpenLayers.Request.POST({
	                url: options.url,
	                callback: this.createCallback(this.handleRead, resp, options),
	                //data: OpenLayers.Util.getParameterString(options.params),
	                data: Ext.JSON.encode(options.params),
	                headers: headers
	            });
	        } else {
	            resp.priv = OpenLayers.Request.GET({
	                url: options.url,
	                callback: this.createCallback(this.handleRead, resp, options),
	                params: options.params,
	                headers: options.headers
	            });
	        }
	        return resp;
	    }
	}),
		
    request     : function(bounds, options) {
		var me = this;
		
		var geographic = new OpenLayers.Projection("EPSG:4326");
		
		bounds.transform(me.map.getProjection(), geographic);
		
		options = options || {
			hover: false
		};

		var filter = new OpenLayers.Filter.Spatial({
	        type: this.filterType, 
	        value: bounds
	    });
	    
	    if(options.hover === false) {
	    	// Set the cursor to "wait" to tell the user we're working.
		    OpenLayers.Element.addClass(this.map.viewPortDiv, "olCursorWait");
	    }
	    
	    var params = {
	        srs    : geographic.toString(),
	    	layers : me.getVisibleLayers()           
	    };
	    
	    var response = this.protocol.read({
	        maxFeatures: options.single == true ? this.maxFeatures : undefined,
	        filter: filter,
	        params : params,
	        callback: function(result) {
	            if(result.success()) {
	            	result.features = me.epsg4326ToMapProjection(result.features);
	            	
	                if(result.features.length) {
	                    if(options.single == true) {
	                        this.selectBestFeature(result.features, bounds.getCenterLonLat(), options);
	                    } else {
	                        this.select(result.features);
	                    }
	                } else if(options.hover) {
	                    this.hoverSelect();
	                } else {
	                    this.events.triggerEvent("clickout");
	                    if(this.clickout) {
	                        this.unselectAll();
	                    }
	                }
	            }
	        	
	            if(options.hover === false) {
		            // Reset the cursor.
		            OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait");
	            }
	        },
	        scope: this
	    });
	    if(options.hover == true) {
	        this.hoverResponse = response;
	    }
	},
	
	getVisibleLayers : function(){
		var me = this;
		
		var layers = [];
		for(var i = 0; i < me.map.layers.length; i++){
			var layer = me.map.layers[i];
			
			if(layer.inRange === true && layer.getVisibility() === true && layer.selecionavel === true &&
				layer.esquema != undefined && layer.tabela != undefined && layer.colunaGeometry != undefined){
				
				layers.push({
					nome           : layer.params.LAYERS,
					esquema        : layer.esquema,
					tabela         : layer.tabela,
					colunaGeometry : layer.colunaGeometry,
					filtro         : layer.filtro
				});
			}
		}
		
		if(me.map.baseLayer.layers && me.map.baseLayer.layers.length > 0){
			for(var i = 0; i < me.map.baseLayer.layers.length; i++){
				var layer = me.map.baseLayer.layers[i];
				
				layer.map = layer.map || me.map;
				
				if(layer.calculateInRange() === true && 
					//layer.getVisibility() === true && 
					layer.selecionavel === true &&
					layer.esquema != undefined && 
					layer.tabela != undefined && 
					layer.colunaGeometry != undefined){
					
					layers.push({
						nome           : layer.params.LAYERS,
						esquema        : layer.esquema,
						tabela         : layer.tabela,
						colunaGeometry : layer.colunaGeometry,
						filtro         : layer.filtro
					});
				}
			}	
		}
		
		return layers;
	},

	epsg4326ToMapProjection: function(features){
		var me = this;
		
		var featuresMapProjection = [];
		
		for(var i = 0; i < features.length; i++){
			var feature = features[i].clone();
			feature.geometry.transform('EPSG:4326', me.map.getProjection());
			
			featuresMapProjection.push(feature);
		}
		
		return featuresMapProjection;
	},
	
	CLASS_NAME: "MapService.Control.SelectFeature"
});